import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:flutter_syt/api/api.dart';
import 'package:flutter_syt/datas/game_subcription_item.dart';
import 'package:flutter_syt/datas/venue_item.dart';
import 'package:flutter_syt/domain/widgets/subscription_manage_item.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/theme/font_styles.dart';
import 'package:provider/provider.dart';

class SubscriptionManageVenueList extends StatefulWidget {
  final ScrollController scrollController;
  const SubscriptionManageVenueList({
    required this.scrollController,
  });

  @override
  _SubscriptionManageVenueListState createState() =>
      _SubscriptionManageVenueListState();
}

class _SubscriptionManageVenueListState
    extends State<SubscriptionManageVenueList> {
  // ScrollController _scrollController = ScrollController();
  List<VenueItem> venues = [];

  initData() async {
    List<VenueItem> data = await API.shared.venue.list();
    setState(() {
      venues = data;
    });
  }

  @override
  void initState() {
    initData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (venues.length == 0) {
      return Container();
    }
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        Text(
          S.of(context).Venues,
          style: FontStyles.headerTextStyle_16.copyWith(
            fontWeight: FontWeight.w500,
          ),
        ),
        SizedBox(
          height: 10,
        ),
        Container(
          child: StaggeredGridView.countBuilder(
            shrinkWrap: true,
            controller: widget.scrollController,
            crossAxisCount: 1,
            crossAxisSpacing: 12,
            mainAxisSpacing: 16,
            itemCount: venues.length,
            itemBuilder: (context, index) {
              VenueItem venue = venues[index];
              return Consumer<GameSubcriptionManageData>(
                  builder: (ctx, data, child) {
                bool subscribed =
                    data.venue.indexWhere((ele) => ele.code == venue.code) !=
                        -1;
                return SubscriptionManageItem(
                  venue.name,
                  mode: SubscriptionManageItemMode.subscribe,
                  subscribed: subscribed,
                  onSubscribe: (name) {
                    Map<String, dynamic> json = {
                      'code': venue.code,
                      'name': venue.name,
                      'id': -1,
                    };
                    GameSubcriptionManageData.add(json, 'venue');
                  },
                );
              });
            },
            staggeredTileBuilder: (index) => StaggeredTile.fit(1),
          ),
        ),
      ],
    );
  }
}
